package cn.star.jots.comments.service.impl;

import cn.star.jots.comments.mapper.CommentsMapper;
import cn.star.jots.comments.pojo.dto.CommentsQuery;
import cn.star.jots.comments.pojo.dto.CommentsSaveParam;
import cn.star.jots.comments.pojo.entity.Comments;
import cn.star.jots.comments.pojo.vo.CommentsVO;
import cn.star.jots.comments.service.CommentsService;
import cn.star.jots.filmRating.mapper.FilmRatingMapper;
import cn.star.jots.filmRating.pojo.entity.FilmRating;
import cn.star.jots.filmRating.pojo.vo.FilmRatingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    CommentsMapper commentsMapper;
    @Autowired
    FilmRatingMapper filmRatingMapper;

    @Override
    public List<CommentsVO> search(CommentsQuery commentsQuery) {
        log.debug("查询评论:commentsQuery={}", commentsQuery);
        return commentsMapper.search(commentsQuery);
    }

    @Override
    public void addComments(CommentsSaveParam commentsSaveParam) {
        log.debug("保存评论:commentsSaveParam={}", commentsSaveParam);
        Comments comments = new Comments();
        BeanUtils.copyProperties(commentsSaveParam, comments);
        comments.setIsDelete("0");
        comments.setCreateTime(new Date());
        commentsMapper.addRoot(comments);

        FilmRatingVO ratings = filmRatingMapper.selectByFilmId(comments.getFilmId());
        if (ratings == null) {
            FilmRating rating = new FilmRating();
            rating.setFilmId(comments.getFilmId());
            rating.setRating(commentsSaveParam.getRating());
            rating.setPeople(1);
            rating.setCreateTime(new Date());
            filmRatingMapper.insert(rating);
        } else {
            FilmRating rating = new FilmRating();
            rating.setId(ratings.getId());
            rating.setPeople(ratings.getPeople() + 1);
            rating.setRating(ratings.getRating() + commentsSaveParam.getRating());
            rating.setFilmRating(ratings.getRating() + commentsSaveParam.getRating() / rating.getPeople() + 1);
            rating.setUpdateTime(new Date());
            filmRatingMapper.update(rating);
        }

    }

    @Override
    public List<CommentsVO> searchAll(CommentsQuery commentsQuery) {
       log.debug("查询所有评论");
       return commentsMapper.searchAll(commentsQuery);
    }


    @Override
    public List<CommentsVO> searchByUserId(String userId) {
        log.debug("查询个人评论:userId={}", userId);
        return commentsMapper.searchByUserId(userId);
    }

    @Override
    public void delete(Long id) {
        log.debug("删除评论:id={}", id);
        Comments comments = new Comments();
        comments.setId(id);
        comments.setIsDelete("1");
        commentsMapper.update(comments);
    }

}
